#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
#include <stdint.h>
#include <fcntl.h> // contact the open(),close(),read(),write() and so on! 
#include <unistd.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>

//frame buffer device point 
#define DEVICE_NAME "/dev/fb0"


int main(int argc,char **argv) 
{    
    int fd;
    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    long int screensize = 0; 
    uint8_t * fbp = 0;
    int ret; 

    printf("\n start framebuffer test \r\n"); 
    fd = open(DEVICE_NAME, O_RDWR); // Open frame buffer device and get the handle 
    printf("fd = %d \n", fd); 
    
    if (fd < 0) //open fail 
    {
        printf("open device %s error \n",DEVICE_NAME); 
    } 
    else 
    { 
        if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo)) // get frame buffer frame_fix_screeninfo
        {
            close(fd);
            return -1;
        }
        
        if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo)) // get frame buffer frame_var_screeninfo
        {
            return -1;
        }      
   
        printf("screensize xres: %d, yres: %d, bits_per_pixel: %d\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
      
        screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
   
        fbp = (uint8_t *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        
        if (fbp == MAP_FAILED) 
        {
            printf("failed to mmap framebuffer!\n");
            close(fd);
            
            return -1;
        }
        
        // memset(fbp, 0xff, screensize);
        
        /*
          vinfo.bits_per_pixel:
          RGB555
          RGB565
          RGB24
          RGB32
        */
        
        for(int i = 0; i < screensize; i += vinfo.bits_per_pixel / 8)
        {
            fbp[i] = 0x00;     // BLUE
            fbp[i + 1] = 0xff; // Green
            fbp[i + 2] = 0x00; // RED
         }

        munmap(fbp, screensize);

        ret = close(fd); //close device 
    } 
    
    return 0; 
}

